/**
 * Copyright (c) qc&Bull Co.,LTD.
 * All right reserved.
 */

package cn.zxd.lis.schema;

import cn.zxd.lis.db.LDSysVarDB;
import cn.zxd.util.*;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * <p>ClassName: LDSysVarSchema </p>
 * <p>Description: DB层 Schema 类文件 </p>
 * <p>Company: qc&Bull Co.,LTD </p>
 * @Database: IIEP_Data_Model
 * @author：anonymous
 * @CreateDate：2015-11-22
 */
public class LDSysVarSchema implements Schema, Cloneable
{
    // @Field
    /** 系统变量名 */
    private String SysVar;
    /** 系统变量类型 */
    private String SysVarType;
    /** 系统变量值 */
    private String SysVarValue;

    /** 数据库表的字段个数 */
    public static final int FIELDNUM = 3;

    /** 主键 */
    private static String[] PK;

    /** 错误信息 */
    public CErrors mErrors;

    // @Constructor
    public LDSysVarSchema()
    {
        mErrors = new CErrors();
        String[] pk = new String[1];
        pk[0] = "SysVar";
        PK = pk;
    }

    /**
     * Schema克隆
     * @return Object
     * @throws CloneNotSupportedException
     */
    public Object clone() throws CloneNotSupportedException
    {
        LDSysVarSchema cloned = (LDSysVarSchema)super.clone();
        cloned.mErrors = (CErrors) mErrors.clone();
        return cloned;
    }

    // @Method
    public String[] getPK()
    {
        return PK;
    }

    public String getSysVar()
    {
        return SysVar;
    }

    public void setSysVar(String aSysVar)
    {
        SysVar = StrTool.cTrim(aSysVar);
    }

    public String getSysVarType()
    {
        return SysVarType;
    }

    public void setSysVarType(String aSysVarType)
    {
        SysVarType = StrTool.cTrim(aSysVarType);
    }

    public String getSysVarValue()
    {
        return SysVarValue;
    }

    public void setSysVarValue(String aSysVarValue)
    {
        SysVarValue = StrTool.cTrim(aSysVarValue);
    }


    /**
     * 使用另外一个 LDSysVarSchema 对象给 Schema 赋值
     * @param: cLDSysVarSchema LDSysVarSchema
     */
    public void setSchema(LDSysVarSchema cLDSysVarSchema)
    {
        this.SysVar = cLDSysVarSchema.getSysVar();
        this.SysVarType = cLDSysVarSchema.getSysVarType();
        this.SysVarValue = cLDSysVarSchema.getSysVarValue();
    }

    /**
     * 使用 ResultSet 中的第 i 行给 Schema 赋值
     * @param: rs ResultSet
     * @param: i int
     * @return: boolean
     */
    public boolean setSchema(ResultSet rs, int i)
    {
        try
        {
            if (rs.getString(1) == null)
                this.SysVar = null;
            else
                this.SysVar = rs.getString(1);
            if (rs.getString(2) == null)
                this.SysVarType = null;
            else
                this.SysVarType = rs.getString(2);
            if (rs.getString(3) == null)
                this.SysVarValue = null;
            else
                this.SysVarValue = rs.getString(3);
        }
        catch (SQLException sqle)
        {
            sqle.printStackTrace();
            // @@错误处理
            CError tError = new CError();
            tError.moduleName = "LDSysVarSchema";
            tError.functionName = "setSchema";
            tError.errorMessage = sqle.toString();
            this.mErrors.addOneError(tError);
            return false;
        }
        return true;
    }

    public LDSysVarSchema getSchema()
    {
        LDSysVarSchema aLDSysVarSchema = new LDSysVarSchema();
        aLDSysVarSchema.setSchema(this);
        return aLDSysVarSchema;
    }

    public LDSysVarDB getDB()
    {
        LDSysVarDB aDBOper = new LDSysVarDB();
        aDBOper.setSchema(this);
        return aDBOper;
    }

    /**
     * 数据打包，按 XML 格式打包
     * @return: String 返回打包后字符串
     */
    public String encode()
    {
        StringBuffer strReturn = new StringBuffer(256);
        strReturn.append(StrTool.cTrim(SysVar));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(SysVarType));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(SysVarValue));
        return strReturn.toString();
    }

    /**
     * 数据解包
     * @param: strMessage String 包含一条纪录数据的字符串
     * @return: boolean
     */
    public boolean decode(String strMessage)
    {
        try
        {
            SysVar = StrTool.getStr(strMessage, 1, SysConst.PACKAGESPILTER);
            SysVarType = StrTool.getStr(strMessage, 2, SysConst.PACKAGESPILTER);
            SysVarValue = StrTool.getStr(strMessage, 3, SysConst.PACKAGESPILTER);
        }
        catch (NumberFormatException ex)
        {
            // @@错误处理
            CError tError = new CError();
            tError.moduleName = "LDSysVarSchema";
            tError.functionName = "decode";
            tError.errorMessage = ex.toString();
            this.mErrors.addOneError(tError);
            return false;
        }
        return true;
    }

    /**
     * 取得对应传入参数的String形式的字段值
     * @param: FCode String 希望取得的字段名
     * @return: String
     * 如果没有对应的字段，返回""
     * 如果字段值为空，返回"null"
     */
    public String getV(String FCode)
    {
        String strReturn = "";
        if (FCode.equalsIgnoreCase("SysVar"))
        {
            strReturn = StrTool.cTrim(SysVar);
        }
        if (FCode.equalsIgnoreCase("SysVarType"))
        {
            strReturn = StrTool.cTrim(SysVarType);
        }
        if (FCode.equalsIgnoreCase("SysVarValue"))
        {
            strReturn = StrTool.cTrim(SysVarValue);
        }
        if (strReturn.equals(""))
        {
            strReturn = "null";
        }
        return strReturn;
    }

    /**
     * 取得Schema中指定索引值所对应的字段值
     * @param: nFieldIndex int 指定的字段索引值
     * @return: String
     * 如果没有对应的字段，返回""
     * 如果字段值为空，返回"null"
     */
    public String getV(int nFieldIndex)
    {
        String strFieldValue = "";
        switch (nFieldIndex)
        {
            case 0:
                strFieldValue = StrTool.cTrim(SysVar);
                break;
            case 1:
                strFieldValue = StrTool.cTrim(SysVarType);
                break;
            case 2:
                strFieldValue = StrTool.cTrim(SysVarValue);
                break;
            default:
                strFieldValue = "";
        }
        if (strFieldValue.equals(""))
        {
            strFieldValue = "null";
        }
        return strFieldValue;
    }

    /**
     * 设置对应传入参数的String形式的字段值
     * @param: FCode String 需要赋值的对象
     * @param: FValue String 要赋的值
     * @return: boolean
     */
    public boolean setV(String FCode, String FValue)
    {
        if (StrTool.cTrim(FCode).equals(""))
            return false;

        if (FCode.equalsIgnoreCase("SysVar"))
        {
            if (FValue != null)
            {
                SysVar = FValue.trim();
            }
            else
                SysVar = null;
        }
        if (FCode.equalsIgnoreCase("SysVarType"))
        {
            if (FValue != null)
            {
                SysVarType = FValue.trim();
            }
            else
                SysVarType = null;
        }
        if (FCode.equalsIgnoreCase("SysVarValue"))
        {
            if (FValue != null)
            {
                SysVarValue = FValue.trim();
            }
            else
                SysVarValue = null;
        }
        return true;
    }

    public boolean equals(Object otherObject)
    {
        if (this == otherObject)
            return true;
        if (otherObject == null)
            return false;
        if (getClass() != otherObject.getClass())
            return false;
        LDSysVarSchema other = (LDSysVarSchema) otherObject;
        return
                (SysVar == null ? other.getSysVar() == null : SysVar.equals(other.getSysVar()))
                        && (SysVarType == null ? other.getSysVarType() == null : SysVarType.equals(other.getSysVarType()))
                        && (SysVarValue == null ? other.getSysVarValue() == null : SysVarValue.equals(other.getSysVarValue()));
    }

    /**
     * 取得Schema拥有字段的数量
     * @return: int
     */
    public int getFieldCount()
    {
        return FIELDNUM;
    }

    /**
     * 取得Schema中指定字段名所对应的索引值
     * 如果没有对应的字段，返回-1
     * @param: strFieldName String
     * @return: int
     */
    public int getFieldIndex(String strFieldName)
    {
        if (strFieldName.equals("SysVar"))
        {
            return 0;
        }
        if (strFieldName.equals("SysVarType"))
        {
            return 1;
        }
        if (strFieldName.equals("SysVarValue"))
        {
            return 2;
        }
        return -1;
    }

    /**
     * 取得Schema中指定索引值所对应的字段名
     * 如果没有对应的字段，返回""
     * @param: nFieldIndex int
     * @return: String
     */
    public String getFieldName(int nFieldIndex)
    {
        String strFieldName = "";
        switch (nFieldIndex)
        {
            case 0:
                strFieldName = "SysVar";
                break;
            case 1:
                strFieldName = "SysVarType";
                break;
            case 2:
                strFieldName = "SysVarValue";
                break;
            default:
                strFieldName = "";
        }
        return strFieldName;
    }

    /**
     * 取得Schema中指定字段名所对应的字段类型
     * 如果没有对应的字段，返回Schema.TYPE_NOFOUND
     * @param: strFieldName String
     * @return: int
     */
    public int getFieldType(String strFieldName)
    {
        if (strFieldName.equals("SysVar"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("SysVarType"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("SysVarValue"))
        {
            return Schema.TYPE_STRING;
        }
        return Schema.TYPE_NOFOUND;
    }

    /**
     * 取得Schema中指定索引值所对应的字段类型
     * 如果没有对应的字段，返回Schema.TYPE_NOFOUND
     * @param: nFieldIndex int
     * @return: int
     */
    public int getFieldType(int nFieldIndex)
    {
        int nFieldType = Schema.TYPE_NOFOUND;
        switch (nFieldIndex)
        {
            case 0:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 1:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 2:
                nFieldType = Schema.TYPE_STRING;
                break;
            default:
                nFieldType = Schema.TYPE_NOFOUND;
        }
        return nFieldType;
    }
}
